import 'dart:ui';

import 'package:flutter_getx/app/config/constant.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class LocaleControl extends GetxController {

  static final LocaleControl to = Get.find();

  static const fallbackLocale = Locale('en', 'US');

  Locale get locale {
    String locale = SpUtil.getString(Constant.locale) ?? '';
    if(locale.isEmpty){
      locale = window.locale.languageCode;
    }
    switch(locale) {
      case 'zh':
        return const Locale('zh', 'CN');
      case 'en':
        return const Locale('en', 'US');
      default:
        return fallbackLocale;
    }
  }

  void setLocale(String loc) {
    SpUtil.putString(Constant.locale, loc);
    Get.updateLocale(locale);
  }

  void switchLocal(){
    if(isZh()){
      setLocale('en');
    }else{
      setLocale('zh');
    }
  }

}

bool isZh(){
  return Get.locale?.languageCode == 'zh';
}